package com.jzo2o.foundations.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.foundations.enums.FoundationStatusEnum;
import com.jzo2o.foundations.mapper.RegionMapper;
import com.jzo2o.foundations.mapper.ServeItemMapper;
import com.jzo2o.foundations.mapper.ServeMapper;
import com.jzo2o.foundations.model.domain.Region;
import com.jzo2o.foundations.model.domain.Serve;
import com.jzo2o.foundations.model.domain.ServeItem;
import com.jzo2o.foundations.model.dto.request.ServePageQueryReqDTO;
import com.jzo2o.foundations.model.dto.request.ServeUpsertReqDTO;
import com.jzo2o.foundations.model.dto.response.ServeResDTO;
import com.jzo2o.foundations.service.IServeService;
import com.jzo2o.mysql.utils.PageHelperUtils;
import io.netty.util.internal.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author DarkClown
 * @date 2024-08-15 20:35
 * @project_name jzo2o-foundations-01-0
 * @parem
 **/
@Service
public class ServeServiceImpl extends ServiceImpl<ServeMapper, Serve> implements IServeService {
    @Autowired
    ServeMapper serveMapper;

    @Autowired
    ServeItemMapper serveItemMapper;

    @Autowired
    RegionMapper regionMapper;

    /**
     * 分页查询服务
     * @param servePageQueryReqDTO
     * @return
     */
    @Override
    public PageResult<ServeResDTO> page(ServePageQueryReqDTO servePageQueryReqDTO) {
//        Page<Serve> servePage = PageUtils.parsePageQuery(servePageQueryReqDTO, Serve.class);
//
//        Page<Serve> listByPage = baseMapper.selectListByPage(servePage,ServeResDTO.class);
//        Page<ServeResDTO> page = new Page<>(servePageQueryReqDTO.getPageNo(), servePageQueryReqDTO.getPageSize());
//        PageResult<ServeResDTO> listByPage = baseMapper.selectListByPage(page, servePageQueryReqDTO);
        PageResult<ServeResDTO> serveResDTOPageResult = PageHelperUtils.selectPage(servePageQueryReqDTO,
                () -> baseMapper.selectListByPage(servePageQueryReqDTO.getRegionId()));

        return serveResDTOPageResult;
    }

    @Override
    public void bachAdd(List<ServeUpsertReqDTO> serveUpsertReqDTOList) {
        for (ServeUpsertReqDTO serveUpsertReqDTO : serveUpsertReqDTOList) {
            //1.校验服务项是否为启用状态，不是启用状态不能新增
            ServeItem serveItem = serveItemMapper.selectById(serveUpsertReqDTO.getServeItemId());
            if(!(serveItem.getActiveStatus() == FoundationStatusEnum.ENABLE.getStatus())){
                throw new ForbiddenOperationException("该服务未启用无法添加到区域下使用");
            }

            //2.校验是否重复新增
            LambdaQueryWrapper<Serve> queryWrapper = Wrappers.<Serve>lambdaQuery()
                    .eq(Serve::getRegionId, serveUpsertReqDTO.getRegionId())
                    .eq(Serve::getServeItemId, serveUpsertReqDTO.getServeItemId());
            Integer count = baseMapper.selectCount(queryWrapper);
            if(count>0){
                throw new ForbiddenOperationException("服务存在重复新增");
            }

            //3.新增服务
            Serve serve = BeanUtil.toBean(serveUpsertReqDTO, Serve.class);
            Region region = regionMapper.selectById(serveUpsertReqDTO.getRegionId());
            serve.setCityCode(region.getCityCode());
            baseMapper.insert(serve);
        }
    }

    @Override
    @Transactional
    public Serve update(Long id, BigDecimal price) {
        //更新服务价格
        boolean update = lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getPrice, price)
                .update();
        if(!update){
            throw new ForbiddenOperationException("修改服务价格失败");
        }
        return baseMapper.selectById(id);

    }

    @Override
    @Transactional
    public Serve onSale(Long id) {
        Serve serve = baseMapper.selectById(id);

        //判断区域服务存不存在
        if (BeanUtil.isEmpty(serve)){
            throw new ForbiddenOperationException("服务不存在");
        }

        //上架状态
        Integer saleStatus = serve.getSaleStatus();

        //草稿或下架状态才可上架
        if (saleStatus!=FoundationStatusEnum.INIT.getStatus()&&saleStatus!=FoundationStatusEnum.DISABLE.getStatus()){
            throw new ForbiddenOperationException("服务不可上架");
        }
        //服务项Id
        Long serveItemId = serve.getServeItemId();
        ServeItem serveItem = serveItemMapper.selectById(serveItemId);
        if (BeanUtil.isEmpty(serveItem)){
            throw new ForbiddenOperationException("服务项不存在");
        }
        //服务项上架状态
        Integer serveItemSaleStatus = serveItem.getActiveStatus();

        //服务项为启用装填可上架
        if (serveItemSaleStatus!=FoundationStatusEnum.ENABLE.getStatus()){
            throw new ForbiddenOperationException("服务项不可上架");
        }

        //更新上架状态
        boolean update = lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getSaleStatus, FoundationStatusEnum.ENABLE.getStatus())
                .update();
        if (!update){
            throw new ForbiddenOperationException("服务上架失败");
        }

        return baseMapper.selectById(id);
    }

    @Override
    public Serve offSale(Long id) {
        //通过id获取服务
        Serve serve = baseMapper.selectById(id);
        //判断服务存不存在
        if (BeanUtil.isEmpty(serve)){
            throw new ForbiddenOperationException("服务不存在");
        }
        //服务状态为上架时才可以下架
        if (serve.getSaleStatus()!=FoundationStatusEnum.ENABLE.getStatus()){
            throw new ForbiddenOperationException("服务不可下架");
        }

        //更新服务状态为下架
        boolean update = lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getSaleStatus, FoundationStatusEnum.DISABLE.getStatus())
                .update();

        if (!update){
            throw new ForbiddenOperationException("服务下架失败");
        }

        return  baseMapper.selectById(id);

    }

    @Override
    public Serve delete(Long id) {
        //通过id获取服务
        Serve serve = baseMapper.selectById(id);
        //判断服务存不存在
        if (BeanUtil.isEmpty(serve)){
            throw new ForbiddenOperationException("服务不存在");
        }
        //状态为草稿状态是才可以删除
        if (serve.getSaleStatus()!=FoundationStatusEnum.INIT.getStatus()){
            throw new ForbiddenOperationException("服务不可删除");
        }

        //根据id删除服务
        int i = baseMapper.deleteById(id);

        if (i<=0){
            throw new ForbiddenOperationException("服务删除失败");
        }
        return baseMapper.selectById(id);


    }

    @Override
    public Serve onHot(Long id) {
        //获取服务
        Serve serve = baseMapper.selectById(id);
        //判断服务存不存在
        if (BeanUtil.isEmpty(serve)){
            throw new ForbiddenOperationException("服务不存在");
        }
        if (serve.getIsHot()==1){
            throw new ForbiddenOperationException("服务已设为热门");
        }
        //根据id修改服务状态为热门
        boolean update = lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getIsHot, 1)
                .update();

        if (!update){
            throw new ForbiddenOperationException("服务设为热门失败");
        }
        return baseMapper.selectById(id);

    }

    @Override
    public Serve offHot(Long id) {
        //获取服务
        Serve serve = baseMapper.selectById(id);
        //判断服务存不存在
        if (BeanUtil.isEmpty(serve)){
            throw new ForbiddenOperationException("服务不存在");
        }
        if (serve.getIsHot()==0){
            throw new ForbiddenOperationException("服务已设为非热门");
        }
        //修改
        boolean update = lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getIsHot, 0)
                .update();
        if (!update){
            throw new ForbiddenOperationException("服务设为非热门失败");
        }
        return baseMapper.selectById(id);
    }


}
